//
//  CreateAccountController.m
//  YiBaoStore
//
//  Created by 吕清毅 on 2018/12/1.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "CreateAccountController.h"
#import "MaintainViewController.h"

@interface CreateAccountController ()<UITextFieldDelegate>
@property (nonatomic,strong) UITextField *phoneTF;
@property (nonatomic,strong) UITextField *codeTF;
@property (nonatomic,strong) UIButton *codeBtn;
@property (nonatomic,strong) UIButton *loginBtn;
@property (nonatomic,assign) BOOL isSelect;
@end

@implementation CreateAccountController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createUIView];
    //    [self changeShopState];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TextFieldChange) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)createUIView
{
    self.view.backgroundColor = TotalWhite;
    self.bgNavView.hidden = YES;
    UIView *navi = [UIView new];
    if (@available(iOS 11.0, *)) {
        navi.frame = CGRectMake(0, 0, SCREENWIDTH, TopBarHeight_xs);
    } else {
        // Fallback on earlier versions
        navi.frame = CGRectMake(0, 0, SCREENWIDTH, TopBarHeight);
    }
    navi.backgroundColor = TotalWhite;
    [self.view addSubview:navi];
    
    UILabel *titleLab = [UILabel new];
    titleLab.frame = CGRectMake(40, navi.bottom+40, SCREENWIDTH-60, 22);
    titleLab.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:2 and:@"请创建一个收款账户"];
    titleLab.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:22];
    titleLab.textColor = UIColorFromHex(0x4D4D4D);
    //titleLab.text = @"登录";
    [self.view addSubview:titleLab];
    
    UILabel *phoneLab = [UILabel new];
    phoneLab.frame = CGRectMake(2*leftPadding, titleLab.bottom+55, 55, 12);
    phoneLab.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:1 and:@"手机号码"];
    phoneLab.textColor = goldColor;
    phoneLab.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:12];
    [self.view addSubview:phoneLab];
    
    self.phoneTF = [UITextField new];
    self.phoneTF.frame = CGRectMake(phoneLab.left, phoneLab.bottom+10, SCREENWIDTH-80, 16);
    self.phoneTF.delegate = self;
    //self.phoneTF.secureTextEntry = YES;
    self.phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    UIButton *button = [self.phoneTF valueForKey:@"_clearButton"];
    [button setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
    self.phoneTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.phoneTF.placeholder = @"请输入手机号码";
    [self.phoneTF setValue:UIColorFromHex(0xD8D8D8) forKeyPath:@"_placeholderLabel.textColor"];
    [self.phoneTF setValue:FontWithSize(16) forKeyPath:@"_placeholderLabel.font"];
    self.phoneTF.returnKeyType = UIReturnKeyNext;
    self.phoneTF.textColor = titleColor;
    self.phoneTF.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
    [self.view addSubview:self.phoneTF];
    
    UIView *lineView = [UIView new];
    lineView.frame = CGRectMake(phoneLab.left, _phoneTF.bottom+10, SCREENWIDTH-80, 1);
    lineView.backgroundColor = BacklineColor;
    [self.view addSubview:lineView];
    
    UILabel *codeLab = [UILabel new];
    codeLab.frame = CGRectMake(2*leftPadding, lineView.bottom+20, 55, 12);
    codeLab.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:1 and:@"验证码"];
    codeLab.textColor = goldColor;
    codeLab.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:12];
    [self.view addSubview:codeLab];
    
    self.codeTF = [UITextField new];
    self.codeTF.frame = CGRectMake(phoneLab.left, codeLab.bottom+10, (SCREENWIDTH-80)*0.6, 16);
    self.codeTF.delegate = self;
    self.codeTF.keyboardType = UIKeyboardTypeNumberPad;
    self.codeTF.returnKeyType = UIReturnKeyDone;
    self.codeTF.textColor = titleColor;
    self.codeTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.codeTF.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
    self.codeTF.placeholder = @"请输入手机验证码";
    [self.codeTF setValue:UIColorFromHex(0xD8D8D8) forKeyPath:@"_placeholderLabel.textColor"];
    [self.codeTF setValue:FontWithSize(16) forKeyPath:@"_placeholderLabel.font"];
    [self.view addSubview:self.codeTF];
    
    UIView *mid = [UIView new];
    mid.frame = CGRectMake(self.codeTF.right, self.codeTF.top+3, 1, 10);
    mid.backgroundColor = BacklineColor;
    [self.view addSubview:mid];
    
    self.codeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat wid = (SCREENWIDTH-80)*0.42;
    self.codeBtn.frame = CGRectMake(mid.right+15, self.codeTF.top, wid-16, 16);
    [self.codeBtn setAttributedTitle:[TTKUtil getAtttributeStringColorWithLineSpace:0 kern:1.33 and:@"发送验证码"] forState:UIControlStateNormal];
    self.codeBtn.titleLabel.font = FontWithSize(16);
    [self.codeBtn addTarget:self action:@selector(getCode:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.codeBtn];
    
    UIView *MidlineView = [UIView new];
    MidlineView.frame = CGRectMake(phoneLab.left, self.codeTF.bottom+10, SCREENWIDTH-80, 1);
    MidlineView.backgroundColor = BacklineColor;
    [self.view addSubview:MidlineView];
    
    self.loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.loginBtn.frame = CGRectMake(2*leftPadding, MidlineView.bottom+90, SCREENWIDTH-80, 50);
    [self.loginBtn setAttributedTitle:[TTKUtil getAtttributeStringColorWithLineSpace:0 kern:1.5 and:@"登录"] forState:UIControlStateNormal];
    self.loginBtn.layer.cornerRadius = 25;
    self.loginBtn.layer.masksToBounds = YES;
    self.loginBtn.backgroundColor = UIColorFromHex(0xD1D1D1);
    self.loginBtn.titleLabel.font = FontWithSize(18);
    [self.loginBtn setTitleColor:UIColorFromHex(0x8D8D8D) forState:UIControlStateNormal];
    [self.loginBtn addTarget:self action:@selector(loginClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.loginBtn];
    
    UIButton *protocBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat left = (SCREENWIDTH-110)/2;
    protocBtn.frame = CGRectMake(left, self.loginBtn.bottom+15, 11, 11);
    protocBtn.selected = YES;
    self.isSelect = YES;
    protocBtn.layer.borderWidth = 1;
    protocBtn.layer.borderColor = UIColorFromHex(0x62616).CGColor;
    protocBtn.backgroundColor = goldColor;
    [protocBtn addTarget:self action:@selector(proBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:protocBtn];
    
    //设置可点击的textview
    ClickTextView *clickTV = [[ClickTextView alloc]initWithFrame:CGRectMake(protocBtn.right+10, self.loginBtn.bottom+7, 125, 25)];
    clickTV.backgroundColor = TotalWhite;
    clickTV.textColor = UIColorFromHex(0x999999);
    clickTV.font = FontMediumWithSize(14);
    NSString *content = @"同意<通联用户协议>";
    clickTV.text = content;
    NSRange range1 = [content rangeOfString:@"通联用户协议"];
    [clickTV setUnderLineTextViewWithRange:range1 withUnderLineColor:goldColor andWithCoverColor:nil withBlock:^(NSString *text) {
        WebViewController *webVC = [WebViewController new];
        webVC.urlString = @"http://serv.eb56.com/html/registration-agreement.html";
        webVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:webVC animated:YES];
    }];
    [self.view addSubview:clickTV];
}

- (void)proBtn:(UIButton *)btn
{
    btn.selected = !btn.selected;
}

#pragma mark -- 验证码
- (void)getCode:(UIButton*)btn
{
    NSString *phone = self.phoneTF.text;
    if([phone isEqualToString:@""])
    {
        [TTKUtil hudShowMessage:@"请输入手机号码"];
        return;
    }
    if (![TTKStringUtils isValidateMobile:phone])
    {
        [TTKUtil hudShowMessage:@"请输入正确的手机号码"];
        return;
    }
    
    NSMutableDictionary *data = [NSMutableDictionary dictionary];
    [data setValue:self.phoneTF.text forKey:@"phone"];
    [HttpRequest formPostPath:TongLianCode_API params:data resultBlock:^(id Data, MsgModel *msgModel) {
        [self ShowHUDWithMessage:@"验证码发送成功"];
        //接口通过才走定时器
        [self startCodeBtnTimeWithBtn:btn];
    }];
}

#pragma mark -- 获取验证码
- (void)startCodeBtnTimeWithBtn:(UIButton *)btn
{
    __block int timeout = 59; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        NSString *title = @"重新发送";
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [btn setAttributedTitle:[TTKUtil getAtttributeStringColorWithLineSpace:0 kern:1.33 and:title] forState:UIControlStateNormal];
                btn.userInteractionEnabled = YES;
            });
        }else{
            int seconds = timeout %60 ;
            NSString *name = @"重新发送";
            NSString *strTime = [NSString stringWithFormat:@"%@%d",name,seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [btn setAttributedTitle:[TTKUtil getAtttributeStringColorWithLineSpace:0 kern:1.33 and:strTime] forState:UIControlStateNormal];
                btn.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    
    dispatch_resume(_timer);
}

- (void)TextFieldChange
{
    NSString *phone = self.phoneTF.text;
    NSString *code = self.codeTF.text;
    if (phone.length > 11) {
        phone = [phone substringToIndex:11];
    }
    if (code.length > 6) {
        code = [code substringToIndex:6];
    }
    if (phone.length>0) {
        self.phoneTF.attributedText = [TTKUtil getAtttributeStringColorWithLineSpace:0 kern:1.33 and:phone];
        self.phoneTF.textColor = titleColor;
    }
    if (code.length>0) {
        self.codeTF.attributedText = [TTKUtil getAtttributeStringColorWithLineSpace:0 kern:1.33 and:code];
        self.codeTF.textColor = titleColor;
    }
    if (phone.length == 11 && code.length == 6) {
        self.loginBtn.userInteractionEnabled = YES;
        self.loginBtn.backgroundColor = goldColor;
        self.loginBtn.titleLabel.textColor = titleColor;
    }else
    {
        self.loginBtn.userInteractionEnabled = NO;
        self.loginBtn.backgroundColor = UIColorFromHex(0xD1D1D1);
        self.loginBtn.titleLabel.textColor = UIColorFromHex(0x8D8D8D);
    }
}

#pragma mark -- 通联账号的登录
- (void)loginClick:(UIButton *)btn
{
    NSMutableDictionary *data = [NSMutableDictionary dictionary];
    [data setValue:self.phoneTF.text forKey:@"phone"];
    [data setValue:self.codeTF.text forKey:@"verificationCode"];
    [HttpRequest formPostPath:TongLianVIP_API params:data resultBlock:^(id Data, MsgModel *msgModel) {
        MaintainViewController *mainVC = [MaintainViewController new];
        [self.navigationController pushViewController:mainVC animated:YES];
    }];
}
@end
